import { unref } from 'vue';
import { isObject } from '@/utils/is';

export const noop = () => {};

/**
 * @description:  设置ui装载节点
 */
export function getPopupContainer(node) {
	return node?.parentNode ?? document.body;
}

/**
 * 将对象作为参数添加到URL
 * @param baseUrl url
 * @param obj
 * @returns {string}
 * eg:
 *  let obj = {a: '3', b: '4'}
 *  setObjToUrlParams('www.baidu.com', obj)
 *  ==>www.baidu.com?a=3&b=4
 */
export function setObjToUrlParams(baseUrl, obj) {
	let parameters = '';
	for (const key in obj) {
		parameters += key + '=' + encodeURIComponent(obj[key]) + '&';
	}
	parameters = parameters.replace(/&$/, '');
	return /\?$/.test(baseUrl) ? baseUrl + parameters : baseUrl.replace(/\/?$/, '?') + parameters;
}

export function objToUrlParams(obj) {
	let parameters = '';
	for (const key in obj) {
		parameters += key + '=' + encodeURIComponent(obj[key]) + '&';
	}
	return (parameters = parameters.replace(/&$/, ''));
}

// 深度合并
export function deepMerge(src = {}, target = {}) {
	let key;
	for (key in target) {
		src[key] = isObject(src[key]) ? deepMerge(src[key], target[key]) : (src[key] = target[key]);
	}
	return src;
}
// 深拷贝
export function deepClone(obj, newObj) {
	var newObj = newObj || {};
	for (key in obj) {
		if (typeof obj[key] == 'object') {
			newObj[key] = obj[key].constructor === Array ? [] : {};
			deepClone(obj[key], newObj[key]);
		} else {
			newObj[key] = obj[key];
		}
	}
	return newObj;
}

export function openWindow(url, opt) {
	const { target = '__blank', noopener = true, noreferrer = true } = opt || {};
	const feature = [];

	noopener && feature.push('noopener=yes');
	noreferrer && feature.push('noreferrer=yes');

	window.open(url, target, feature.join(','));
}

// 动态使用钩道具
export function getDynamicProps(props) {
	const ret = {};

	Object.keys(props).map((key) => {
		ret[key] = unref(props[key]);
	});

	return ret;
}

export function getRawRoute(route) {
	if (!route) return route;
	const { matched, ...opt } = route;
	return {
		...opt,
		matched: matched
			? matched.map((item) => ({
					meta: item.meta,
					name: item.name,
					path: item.path,
			  }))
			: undefined,
	};
}

export const withInstall = (component, alias) => {
	const comp = component;
	comp.install = (app) => {
		app.component(comp.name || comp.displayName, component);
		if (alias) {
			app.config.globalProperties[alias] = component;
		}
	};
	return component;
};

/**
 * 将通过的百分比求和到十六进制颜色的R、G或B
 * @param {string} color The color to change
 * @param {number} amount The amount to change the color by
 * @returns {string} The processed part of the color
 */
function addLight(color, amount) {
	const cc = parseInt(color, 16) + amount;
	const c = cc > 255 ? 255 : cc;
	return c.toString(16).length > 1 ? c.toString(16) : `0${c.toString(16)}`;
}

/**
 * 根据通过的百分比点亮6个字符的十六进制颜色
 * @param {string} color 要更改的颜色
 * @param {number} amount 决定改变颜色
 * @returns {string} 处理后的颜色表示为十六进制
 */
export function lighten(color, amount) {
	color = color.indexOf('#') >= 0 ? color.substring(1, color.length) : color;
	amount = Math.trunc((255 * amount) / 100);
	return `#${addLight(color.substring(0, 2), amount)}${addLight(color.substring(2, 4), amount)}${addLight(color.substring(4, 6), amount)}`;
}

/**
 * @param {string} s 需要校验的字符串
 * @param {string} p 通配符
 * @return {boolean}
 */
export function isMatch(s, p) {
	// 构造 dp 函数
	let dp = [];
	for (let i = 0; i <= s.length; i++) {
		let child = [];
		for (let j = 0; j <= p.length; j++) {
			child.push(false);
		}
		dp.push(child);
	}
	dp[s.length][p.length] = true;
	// 执行
	for (let i = p.length - 1; i >= 0; i--) {
		if (p[i] != '*') break;
		else dp[s.length][i] = true;
	}

	for (let i = s.length - 1; i >= 0; i--) {
		for (let j = p.length - 1; j >= 0; j--) {
			if (s[i] == p[j] || p[j] == '?') {
				dp[i][j] = dp[i + 1][j + 1];
			} else if (p[j] == '*') {
				dp[i][j] = dp[i + 1][j] || dp[i][j + 1];
			} else {
				dp[i][j] = false;
			}
		}
	}
	return dp[0][0];
}

// 生成随机id
export const genRandomId = () => Number(Math.random().toString().substring(3, 5) + Date.now()).toString(36);

const hexList = [];
for (let i = 0; i <= 15; i++) {
	hexList[i] = i.toString(16);
}

export function buildUUID() {
	let uuid = '';
	for (let i = 1; i <= 36; i++) {
		if (i === 9 || i === 14 || i === 19 || i === 24) {
			uuid += '-';
		} else if (i === 15) {
			uuid += 4;
		} else if (i === 20) {
			uuid += hexList[(Math.random() * 4) | 8];
		} else {
			uuid += hexList[(Math.random() * 16) | 0];
		}
	}
	return uuid.replace(/-/g, '');
}

let unique = 0;
export function buildShortUUID(prefix = '') {
	const time = Date.now();
	const random = Math.floor(Math.random() * 1000000000);
	unique++;
	return prefix + '_' + random + unique + String(time);
}
